package com.pet.server.ext.service;

import java.util.List;

import com.pet.server.ext.ApiError;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Article;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.Subject;
import com.pet.server.ext.data.UserArticle;
import com.pet.server.ext.dbutil.JdbcTemplate;
import com.pet.server.ext.dbutil.Sql;


public class ArticleService {
	private JdbcTemplate jdbcTemplate = new JdbcTemplate();

	public List<Constant> queryConstant(String pcode) throws ApiError{
		Sql sql = new Sql("SELECT * FROM constant c WHERE c.pcode=? ORDER BY sort DESC");
		    sql.addParam(pcode);
		return jdbcTemplate.queryForList(sql, Constant.class);
	}
	public List<Constant> queryConstantByCate(String cate) throws ApiError{
		Sql sql = new Sql("SELECT c.* FROM constant c,cate_brand cb WHERE cb.brand=c.code AND cb.cate=?");
		    sql.addParam(cate);
		    sql.append(" ORDER BY c.sort DESC");
		    return jdbcTemplate.queryForList(sql, Constant.class);
	}
	
	public void searchArticle(Page page,String cate,String age_cate,String pet_cate,Integer subject_id) throws ApiError{
		Sql sql = new Sql("SELECT a.*,c.conts_name as cate_name FROM article a,constant c WHERE a.cate=c.code and a.state=? ");
		    sql.addParam(BaseAttribute.LOGIC_TRUE);
		    sql.appendLike("^", "AND", "a.cate", cate);
		    sql.appendLike("^", "AND", "a.age_cate", age_cate);
		    sql.appendLike("^", "AND", "a.pet_cate", pet_cate);
		    sql.append("AND", "a.subject_id", "=", subject_id);
		    sql.append("  ORDER BY a.article_id DESC");
		jdbcTemplate.search(page, sql, Article.class);
	}
	public List<Article> queryArticleForIndex(String cate,String pet_cate,String age_cate) throws ApiError{
		Sql sql = new Sql("SELECT a.*,c.conts_name as cate_name FROM article a,constant c WHERE a.cate=c.code and a.state=? ");
		    sql.addParam(BaseAttribute.LOGIC_TRUE);
		    sql.appendLike("^", "AND", "a.cate", cate);
		    sql.append("  ORDER BY sort DESC");
		return jdbcTemplate.queryForList(sql, Article.class,5);
	}
	public Article findArticle(Integer id) throws ApiError{
		Sql sql = new Sql("SELECT a.* FROM article a,constant c WHERE a.article_id=?");
		    sql.addParam(id);
	    return  jdbcTemplate.findObject(sql, Article.class);
	}
	
	public List<Article> queryArticleForFind(String[] brands,String[] ageCate,String[] articleCate,Integer size) throws ApiError{
		Sql sql = new Sql("SELECT a.* FROM article a WHERE 1=1 ");
		    sql.appendIn("AND", "a.pet_cate", brands);
		    sql.appendIn("AND", "a.age_cate", ageCate);
		    sql.appendIn("AND", "a.cate", articleCate);
		    sql.append(" ORDER BY a.article_id DESC ");
		return jdbcTemplate.queryForList(sql, Article.class, size);
	}
	public List<Article> queryArticleForFind(String brands,String ageCate,Integer size,List<Integer> ids) throws ApiError{
		Sql sql = new Sql("SELECT a.* FROM article a WHERE 1=1 ");
		    sql.append("AND", "a.pet_cate", "=", brands);
		    sql.append("AND", "a.age_cate", "=", ageCate);
		    sql.appendNotIn("AND", "a.article_id", ids);
	    sql.append(" ORDER BY a.article_id DESC ");
	return jdbcTemplate.queryForList(sql, Article.class, size);
	}
	public List<Article> queryArticle(List<Integer> ids,Integer size) throws ApiError{
		Sql sql = new Sql("SELECT a.* FROM article a WHERE 1=1 ");
		    sql.appendNotIn("AND", "a.article_id", ids);
		    sql.append(" ORDER BY a.article_id DESC ");
		return jdbcTemplate.queryForList(sql, Article.class, size);
	}
	
	public List<Subject> querySubject(Integer subject_size) throws ApiError{
		Sql sql = new Sql("SELECT * FROM `subject` s WHERE s.state=?");
	        sql.addParam(BaseAttribute.LOGIC_TRUE);
		    sql.append("ORDER BY s.id DESC");
		return jdbcTemplate.queryForList(sql, Subject.class,subject_size);
	}
	
	public void searchSubject(Page page) throws ApiError{
		Sql sql = new Sql("SELECT * FROM `subject` s WHERE s.state=?");
		    sql.addParam(BaseAttribute.LOGIC_TRUE);
		    sql.append("ORDER BY s.id DESC");
		jdbcTemplate.search(page, sql, Subject.class);
	}
	
	public void saveUserArticle(UserArticle ua) throws ApiError{
		Sql sql = new Sql("INSERT INTO pet.user_article (article_id, user_id) VALUES (?,?) ON DUPLICATE KEY UPDATE create_date=NOW()");
		    sql.addParam(ua.article_id,ua.user_id);
		jdbcTemplate.saveObject(sql);
	}
	public Integer deleteUserArticle(Integer userId,Integer articleId) throws ApiError{
		Sql sql = new Sql("DELETE ua FROM user_article ua WHERE ua.article_id=? AND ua.user_id=?");
	        sql.addParam(articleId,userId);
	    return jdbcTemplate.updateObject(sql);
	}
	
	
	public void searchArtilceByFocus(Page page,Integer userId) throws ApiError{
		Sql sql =new Sql("SELECT a.* FROM article a,user_article ua WHERE a.article_id=ua.article_id AND ua.user_id=?");
		    sql.addParam(userId);
		    sql.append(" ORDER BY a.article_id DESC ");
	     jdbcTemplate.search(page, sql, Article.class);
	}
	
	
}
